package com.aaa.hospital.Configurationterminal.controller;

import com.aaa.hospital.Client.pojo.Visitingcard;
import com.aaa.hospital.Client.service.Visitingcard_xyService;
import com.aaa.hospital.Configurationterminal.pojo.Admintab;
import com.aaa.hospital.Configurationterminal.pojo.Registermx;
import com.aaa.hospital.Configurationterminal.service.LoginService;
import com.aaa.hospital.ManagementEnd.pojo.Docuser;
import com.aaa.hospital.ManagementEnd.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
//徐要磊
@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private Visitingcard_xyService visitingcard_xyService;


//    登录页面
    @RequestMapping("/loginon")
    @ResponseBody
    public String login(String user, String password, HttpSession session){
        System.out.println("账号  "+user+"  密码  "+password);
        System.out.println("李高强是弟弟");
        List<User> list=loginService.UsrerqueryById(user, password);
        List<Docuser> list1=loginService.DocuserqueryById(user, password);
        List<Admintab> list2=loginService.AdmintabqueryById(user, password);

        List<Registermx> list4=loginService.RegistermxById(user, password);

        if(list.size()>0){

            List<User> list3=  loginService.UsrerqueryBYName(user);
            Integer a=Integer.parseInt(user);
            List<Visitingcard> list5= visitingcard_xyService.queryByName(a);
            //登录到用户页面
            session.setAttribute("user",user);
            session.setAttribute("username",list3.get(0).getUname());
            session.setAttribute("visid",list5.get(0).getVisid());


            session.setAttribute("logon_rights",1);
            return "1";
        }else if(list1.size()>0){
            //登录到医生页面
            List<Docuser> list3=  loginService.DocuserqueryBYName(user);
            session.setAttribute("user",user);
            session.setAttribute("username",list3.get(0).getName());
            session.setAttribute("logon_rights",2);

            return "2";
        }else if(list2.size()>0){
            //登录到后台管理页面

            List<Admintab> list3=  loginService.AdmintabqueryBYName(user);
            session.setAttribute("user",user);
            session.setAttribute("username",list3.get(0).getName());
            session.setAttribute("logon_rights",3);

            return "3";
        }else if(list4.size()>0){
            //登录到护士页面

            List<Registermx> list3=  loginService.RegistermxBYName(user);
            session.setAttribute("user",user);
            session.setAttribute("username",list3.get(0).getName());
            session.setAttribute("logon_rights",4);

            return "4";
        }else {
            return "0";
        }

    }

//    主页获取登录信息
    @RequestMapping("/loginin")
    @ResponseBody
    public String loginin(HttpSession session){

       if(session.getAttribute("username")!=null){
           String name= (String) session.getAttribute("username");
           System.out.println("登录的用户"+name);
           return name;
       }else{
           System.out.println("登录的用户为空");
           return "0";
       }

    }

//退出登录
    @RequestMapping("/loginout")
    @ResponseBody
    public String loginout(HttpSession session){
        session.removeAttribute("username");
        session.removeAttribute("logon_rights");
        return "true";
    }

    @RequestMapping("/loginout1")
    @ResponseBody
    public String loginout1(HttpSession session){

       try{
           Integer a= (Integer) session.getAttribute("logon_rights");
           if(a==2){
               return "0";

           }else{
               return "1";

           }
       }catch (Exception e){

           return "2";
       }

    }


    @RequestMapping("/loginout2")
    @ResponseBody
    public String loginout2(HttpSession session){

        try{
            Integer a= (Integer) session.getAttribute("logon_rights");
            if(a==3){
                return "0";

            }else{
                return "1";

            }
        }catch (Exception e){

            return "2";
        }

    }

    @RequestMapping("/loginout3")
    @ResponseBody
    public String loginout3(HttpSession session){

        try{
            Integer a= (Integer) session.getAttribute("logon_rights");
            if(a==4){
                return "0";

            }else{
                return "1";

            }
        }catch (Exception e){

            return "2";
        }

    }

    @RequestMapping("/loginout4")
    @ResponseBody
    public String loginout4(HttpSession session){

        try{
            Integer a= (Integer) session.getAttribute("logon_rights");
            if(a==1){
                return "0";

            }else{
                return "1";

            }
        }catch (Exception e){

            return "2";
        }

    }


    @RequestMapping("/loginName")
    @ResponseBody
    public String loginName(HttpSession session) {
        String a= (String) session.getAttribute("username");

        return   a;
    }
}
